<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>

<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理
            <small>预约订单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>预约订单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">

                <el-date-picker
                        v-model="pagination.queryDate"
                        type="daterange"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                </el-date-picker>
                <el-select v-model="pagination.queryOrderType" placeholder="请选择">
                    <el-option
                            v-for="item in typeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
                <el-select v-model="pagination.queryOrderStatus" placeholder="请选择">
                    <el-option
                            v-for="item in statusOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>

                <el-input placeholder="会员姓名/手机号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleOrder()">预约</el-button>
                <el-button type="primary" class="butT" @click="clearCondition()">清除</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="member.name" label="会员姓名" align="center"></el-table-column>
                <el-table-column prop="member.phoneNumber" label="手机号码" align="center"></el-table-column>

                <el-table-column prop="orderDate" label="预约日期" align="center"></el-table-column>
                <el-table-column prop="orderType" label="预约类型" align="center"></el-table-column>

                <el-table-column label="预约状态" align="center">

                    <template slot-scope="scope">
                        <el-switch
                                style="display: block"
                                v-model="dataList[scope.$index].orderStatus"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                :inactive-text="showName(dataList[scope.$index].orderStatus)"
                                @change="statusEdit(scope.row)"
                        >
                        </el-switch>
                    </template>


                </el-table-column>

                <el-table-column prop="setmeal.name" label="套餐名称" align="center">

                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" id="edit" @click="handleUpdate(scope.row)" >编辑</el-button>
                        <el-button size="mini" type="danger" @click="handDelete(scope.row)" >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page.sync="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增预约" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="orderForm" :model="formData" label-position="right" label-width="100px"
                                         :rules="rules">
                                    <el-row>
                                        <el-col :span="11">
                                            <el-form-item label="手机号码" placeholder="请输入手机号码" prop="phoneNumber">
                                                <el-input v-model="formData.phoneNumber"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="11">
                                            <el-form-item label="预约日期">
                                                <div class="block">
                                                    <span class="demonstration"></span>
                                                    <el-date-picker
                                                            v-model="formData.orderDate"
                                                            type="date"
                                                            value-format="yyyy-MM-dd"
                                                            placeholder="请选择日期"
                                                            :picker-options="pickerOptions"
                                                            style="width: 150px">
                                                    </el-date-picker>
                                                </div>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="11">
                                            <el-form-item label="姓名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <!--&lt;!&ndash;新增医院地址项&ndash;&gt;
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="医院地址" >
                                                <el-select v-model="addresslist.dept">
                                                    <el-option
                                                            v-for="address in addresslist"
                                                            :key="address.id"
                                                            :label="address.dept"
                                                            :value="address.addressName"
                                                    ></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>-->

                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="套餐信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>套餐编码</th>
                                            <th>套餐名称</th>
                                            <th>套餐说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="setmealIds" type="checkbox" :value="c.id"
                                                       @change="changeCheck(c.id)">
                                            </td>
                                            <td><label :for="c.id">{{c.code}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.remark}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑预约" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" :model="editFormData" label-position="right"
                                         label-width="100px"
                                         :rules="rules">
                                    <el-row>
                                        <el-col :span="11">
                                            <el-form-item label="姓名">
                                                <el-input v-model="dataEditForm.memberName"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="11">
                                            <el-form-item label="预约日期">
                                                <div class="block">
                                                    <span class="demonstration"></span>
                                                    <el-date-picker
                                                            v-model="dataEditForm.orderDate"
                                                            type="date"
                                                            unlink-panels
                                                            value-format="yyyy-MM-dd"
                                                            placeholder="请选择日期"
                                                            :picker-options="pickerOptions"
                                                            style="width: 150px">
                                                    </el-date-picker>
                                                </div>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="11">
                                            <el-form-item label="预约类型">
                                                <el-select v-model="dataEditForm.orderType" placeholder="请选择">
                                                    <el-option
                                                            v-for="item in typeOptions"
                                                            :key="item.value"
                                                            :label="item.label"
                                                            :value="item.value">
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="11">
                                            <el-form-item label="预约状态">
                                                <el-select v-model="dataEditForm.orderStatus" placeholder="请选择">
                                                    <el-option
                                                            v-for="item in statusOptions"
                                                            :key="item.value"
                                                            :label="item.label"
                                                            :value="item.value">
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <!-- &lt;!&ndash;编辑医院地址项&ndash;&gt;
                                     <el-row>
                                         <el-col :span="24">
                                             <el-form-item label="医院地址" >
                                                 <el-select v-model="editFormData.dept">
                                                     <el-option
                                                             v-for="address in addresslist"
                                                             :key="address.id"
                                                             :label="address.dept"
                                                             :value="address.addressName"
                                                     ></el-option>
                                                 </el-select>
                                             </el-form-item>
                                         </el-col>
                                     </el-row>-->

                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="套餐信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>套餐</th>
                                            <th>年龄</th>
                                            <th>详细说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="setmealIds" type="checkbox" :value="c.id"
                                                       @change="changeCheck(c.id)">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.age}}</label></td>
                                            <td><label :for="c.id">{{c.remark}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>


    var validatePhone = (rule, value, callback) => {
        var phoneNumberValue = vue.formData.phoneNumber;
        var reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!reg.test(phoneNumberValue)) {
            callback(new Error("请填写正确的手机号码"));
        } else {

            callback();
        }
    };
    var vue = new Vue({
        el: '#app',
        data: {
            //本页面按钮显示需要的权限
            permission:{
            },

            //保证只能在今天之后预约
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() < Date.now() - 8.64e7;
                }
            },
            // id:,//预约信息行id
            phoneNumber: "",//手机号码
            checked: null,//单选框的选中
            typeOptions: [{
                value: '微信预约',
                label: '微信预约'
            }, {
                value: '手机预约',
                label: '手机预约'
            }],
            statusOptions: [{
                value: '未到诊',
                label: '未到诊'
            }, {
                value: '已到诊',
                label: '已到诊'
            }],
            rules: {//校验规则
                phoneNumber: [{validator: validatePhone, trigger: 'blur'}]
            },
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                queryDate: [],
                queryOrderType: null,
                queryOrderStatus: null,
                currentPage: 1,
                pageSize: 10,
                total: 0,
                queryString: null,
            },
            dataList: [],//列表数据,状态使用
            formData: {},//表单数据
            editFormData: {},//编辑窗口数据
            tableData: [],//添加表单窗口中检查组列表数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//编辑窗口显示问题
            setmealIds: [],//新增和编辑表单中套餐对应的单选框，基于双向绑定可以进行回显和数据提交
            setmealId: [], //因为添加和修改套餐都只是一个套餐,所以此处接收一个数据
            addresslist:[],//所有机构地址的集合
            dataEditForm:{}


        },
        created() {
            //发送ajax请求向后端获取到该用户拥有的权限
            axios.post("/permissionserver/findAllPermission.do").then((res) => {
                this.permission = res.data;
            });

            this.findPage();
        },


        methods: {
            //查询条件清除
            clearCondition() {
                this.pagination.queryString = null;
                this.pagination.queryOrderStatus = null;
                this.pagination.queryOrderType = null;
                this.pagination.queryDate = null;
                this.findPage();
            },

            //switch开关编辑预约状态
            statusEdit(row) {
                axios.post("/order/statusEdit.do?name="+row.id).then(res => {
                    if (res.data.flag) {
                        //编辑预约状态成功提示
                        this.$message.success(res.data.message);
                        //查询页面
                        this.findPage()
                    }
                })
            },

            showName(orderStatus) {
                //把未到诊和已到诊和true false 对应起来
                return orderStatus ? "未到诊" : "已到诊";
            },

            //解决单选框问题
            changeCheck(cid) {
                if (this.setmealIds.length > 1) {
                    for (let i = 0; i < this.setmealIds.length; i++) {
                        let setmentId = this.setmealIds[i];
                        if (setmentId != cid) {
                            this.setmealIds[i] = "";
                        }
                    }
                }
            },


            //弹出修改预约窗口 回显信息
            handleUpdate(row) {
                console.log(row);
                this.dialogFormVisible4Edit = true;
                if (!row.orderStatus) {
                    this.dialogFormVisible4Edit = false;
                    this.$message.error("已到诊用户套餐信息不可更改!");
                    return;
                }
                //拿着id回显基本信息
                //编辑时 id作为一条用户数据的唯一标识
                this.phoneNumber = row.phoneNumber;
                this.activeName = 'first';
                this.setmealIds = [];
                //订单信息和套餐ID
                axios.get("/order/findOrderById.do?id="+row.id).then(res => {
                    if (res.data.flag) {
                        //编辑窗口弹出
                        this.dialogFormVisible4Edit = true;
                        //编辑窗口表单信息的回显
                        this.dataEditForm = res.data.data;

                        console.log("---------------------------");
                        console.log(this.dataEditForm );
                        console.log("---------------------------");
                    } else {
                        //回显基本信息失败提示
                        this.$message.error(res.data.message)
                    }

                });
                //回显套餐信息
                axios.get("/setmeal/findAll.do").then(res => {
                    if (res.data.flag) {
                        this.tableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message)
                    }
                });
                /*//发送ajax查询所有机构的地址
                axios.get("/address/findAllAddress.do").then((res)=>{
                    if(res.data.flag){
                        console.log(res.data.data);
                        this.addresslist=res.data.data;
                    }
                });*/
                //请求关联的套餐ID集合
                axios.get("/order/findSetmealIdsByOrderId.do?id=" + row.id).then((resp) => {
                    if (resp.data.flag) {
                        this.setmealIds = resp.data.data;
                    } else {
                        this.$message.error(resp.data.message);
                    }
                }).catch((resp) => {
                    this.$message.error(resp.data.message);
                })
            },

            //修改预约信息
            handleEdit() {
                //隐藏弹窗
                this.dialogFormVisible4Edit = false;
                this.$refs['dataEditForm'].validate((valid) => {
                    var setmealId = this.setmealIds[this.setmealIds.length - 1];
                    console.log("-----"+valid+"-----------------" );
                    if (valid) {
                        console.log("---------------------------");
                        console.log(this.dataEditForm );
                        console.log("---------------------------");
                        axios.post("/order/edit.do?setmealId=" + setmealId, {
                            //在编辑预约信息之前,页面已做过基本信息和套餐信息的回显
                            //将修改后的套餐id 会员姓名 预约时间 预约日期 预约状态 手机号码（作为会员的标识）
                            name: this.dataEditForm.memberName,
                            orderDate: this.dataEditForm.orderDate,
                            orderType: this.dataEditForm.orderType,
                            orderStatus: this.dataEditForm.orderStatus,
                            phoneNumber: this.dataEditForm.phoneNumber,
                            id: this.id,
                            orderId: this.dataEditForm.id,
                        }).then(res => {
                            if (res.data.flag) {
                                //如果成功
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            } else {
                                //编辑失败提示
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            //不管成功获取失败都掉下分页
                            this.findPage();
                        });
                    } else {
                        //校验不通过
                        this.$message.error("表单数据校验失败");
                        //停止代码执行
                        return false;
                    }
                })
            },

            // 弹出新增预约窗口
            handleOrder() {
                //对编辑过的信息重置
                this.resetForm();
                //所有的选中套餐的ID重置
                this.setmealIds = [];
                //显示第一个tab
                this.activeName = 'first';
                //弹出窗口
                this.dialogFormVisible = true;
                //发送ajax查询所有机构的地址
                axios.get("/address/findAll.do").then((res)=>{
                    if(res.data.flag){
                        this.addresslist=res.data.data;
                    }

                });

                //发送ajax请求 回显套餐信息数据
                axios.get("/setmeal/findAll.do").then(res => {
                    if (res.data.flag) {
                        //数据存储在tabaleData 用于套餐信息回显
                        this.tableData = res.data.data;
                    } else {
                        //回显数据错误提示
                        this.$message.error(res.data.message)
                    }
                }).catch((response) => {
                    console.log(error);
                    this.$message.error(response.data.message);
                });
            },

            //新增预约
            handleAdd() {
                //进行表单校验
                this.$refs['orderForm'].validate(valid => {
                    if (valid) {
                        //传过去的数据 手机号码 预约时间 姓名 套餐id
                        var param = {phoneNumber: this.formData.phoneNumber,
                            orderDate: this.formData.orderDate,
                            name: this.formData.name,}
                        //如果表单检验通过，发送ajax请求
                        axios.post("/order/add.do?setmealIds="+this.setmealIds[0],param).then(res => {
                            if (res.data.flag) {
                                //增加预约成功提示
                                this.$message.success(res.data.message);
                            } else {
                                //增加预约错误提示
                                this.$message.error(res.data.message)
                            }
                        }).finally(() => {
                            //预约窗口关闭
                            this.dialogFormVisible = false;
                            //查询页面
                            this.findPage();
                        })
                    } else {
                        //表单检验未通过
                        this.$message.error("表单数据有误,请检查输入是否正确！")
                        return false;
                    }
                })
            },

            //删除预约信息
            handDelete(row) {
                //删除确认
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    axios.get("/order/delete.do?id=" + row.id).then(res => {
                        if (res.data.flag) {
                            //删除预约信息成功提示
                            this.$message.success(res.data.message);
                            //查询页面
                            this.findPage();
                        } else {
                            //删除预约信息失败提示
                            this.$message.error(res.data.message)
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            //分页查询
            findPage() {
                var param = {
                    queryDate:this.pagination.queryDate,
                    queryOrderType: this.pagination.queryOrderType,
                    queryOrderStatus: this.pagination.queryOrderStatus,
                    currentPage: this.pagination.currentPage,//页码
                    pageSize: this.pagination.pageSize,//每页显示的条数
                    queryString: this.pagination.queryString//查询条件
                };
                //发送ajax请求,给dataList写回数据
                axios.post("/order/findPageOrder.do", param).then((res) => {
                    this.dataList = res.data.rows;
                    for (var i = 0; i < this.dataList.length; i++) {
                        if (this.dataList[i].orderStatus == "未到诊") {
                            this.dataList[i].orderStatus = true;
                        } else {
                            this.dataList[i].orderStatus = false;
                        }
                    }
                    this.pagination.total = res.data.total;
                });
            },

            // 重置表单
            resetForm() {
                this.formData = {};
            },


            //切换页码
            handleCurrentChange() {
                //这里页码已经同步
                this.findPage()
            }
        }
    });


</script>
</html>